using Gee;

namespace Vwf.Vwt {
	public class BlockControl : Block {
		public string className = null;
		public string controlName = null;
		public ArrayList<BlockVala> properties = new ArrayList<BlockVala>();
		
		/* public class Property {
			public string name { get; set; }
			public string content { get; set; }
			public PropertyType type { get; set; }
			public enum Type { Expression, Callback };
			
			public Property(string iName, string iContent, Property.Type iType) {
				name = iName;
				content = iContent;
				type = iType;
			}
		}*/
		/* public override string? codeRender() {
			return code != null ? "response.write(\"" + Tools.Vala.EncodeValaString(code) + "\");" : null;
		} */
		public static int uNameNum = 0;
		
		public static int getNextNameNum() {
			uNameNum++;
			return uNameNum;
		}
		
		public override Code getCode() {
			Code code = new Code();
			
			if (controlName == null) {
				controlName = "___c" + getNextNameNum().to_string();
			}
			code.init = "ui." + controlName + " = new " + className + "();\n";
			foreach(var item in properties) {
				if (item.type == BlockVala.Type.Delegate) {
					string name = "___" + controlName + "_" + item.name;
					code.init += "ui." + controlName + "." + item.name + " = " + name + ";\n";
					code.uiMethods = "protected void " + name + "(Control control, ControlEventArgs event) {\n" + item.code + "\n}\n";
				} else {
					code.init += "ui." + controlName + "." + item.name + " = " + item.code + ";\n";
				}
			}			
			code.init += "}";
			
			code.uiProperties += className + " " + controlName + ";\n";
			
			return code;
			/* string ret = className + ":\n";
			foreach(var item in properties) {
				ret += "\n" + item.name + ": " + item.code;
			} * /
			string ret = "{\n\tvar item = new " + className + "();\n";
			foreach(var item in properties) {
				if (item.type == BlockVala.Type.Delegate) {
					
				} else {
					ret += "\titem." + item.name + " = " + item.code + ";\n";
				}
			}
			
			return ret; */
		}
		public override string? codeUiClassProperty() {
			
		}
		public BlockControl() {
		
		}
		public void addProperty(string iName, string iCode, BlockVala.Type iType) {
			stdout.printf("addProperty(" + iName + ", " + iCode +") \n");
			properties.add(new BlockVala() { name = iName, code = iCode, type = iType });
		}
	}
}
